﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace cmdrunner
{
    public partial class Form3 : Form
    {
        public static readonly int REFRESH_INTERVAL = 5000;

        private int CurrentPID = Process.GetCurrentProcess().Id;

        private Timer timer;

        public Form3()
        {
            InitializeComponent();

            FormClosing += Form3_FormClosing;

            textBox1.KeyDown += TextBox1_KeyDown;
            textBox1.Click += TextBox1_Click;
            textBox1.Text = Convert.ToString(CurrentPID);

            checkBox1.CheckedChanged += CheckBox1_CheckedChanged;

            treeView1.NodeMouseDoubleClick += TreeView1_NodeMouseDoubleClick;

            LoadProcess(CurrentPID);
        }

        private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            StopAutoRefresh();
        }

        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                StartAutoRefresh();
            }
            else
            {
                StopAutoRefresh();
            }
        }

        private void TreeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            textBox1.Text = Convert.ToString(e.Node.Tag);
            LoadProcess(Convert.ToInt32(e.Node.Tag));
        }

        private void TextBox1_Click(object sender, EventArgs e)
        {
            textBox1.SelectAll();
        }

        private void TextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                LoadProcess();
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoadProcess();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void LoadProcess()
        {
            int pid = -1;

            if (textBox1.Text.Length == 0)
            {
                pid = CurrentPID;
                textBox1.Text = Convert.ToString(CurrentPID);
            }
            else
            {
                try
                {
                    pid = Convert.ToInt32(textBox1.Text);
                }
                catch
                {
                    MessageBox.Show("只能输入数字");
                    textBox1.Focus();
                    return;
                }

            }

            if (pid >= 0)
            {
                LoadProcess(pid);

            }
        }

        private void LoadProcess(int pid)
        {

            treeView1.Nodes.Clear();

            List<ProcessInfo> parents = Util.FindParents(pid);

            TreeNode parentNode = null;
            foreach (ProcessInfo parent in parents)
            {
                TreeNode treeNode = NewTreeNode(parent);
                if (parentNode == null)
                {
                    treeView1.Nodes.Add(treeNode);
                }
                else
                {
                    parentNode.Nodes.Add(treeNode);
                }
                parentNode = treeNode;
            }

            ProcessInfo p = Util.FindProcess(pid, true);
            if (p == null)
            {
                treeView1.ExpandAll();
                return;
            }

            TreeNode thisNode = NewTreeNode(p);
            thisNode.BackColor = Color.LightBlue;

            if (parentNode == null)
            {
                treeView1.Nodes.Add(thisNode);
            }
            else
            {
                parentNode.Nodes.Add(thisNode);
            }

            foreach (ProcessInfo child in p.Children)
            {
                TreeNode treeNode = NewTreeNodeRecursive(child);
                thisNode.Nodes.Add(treeNode);
            }

            treeView1.ExpandAll();
            treeView1.Nodes[0].EnsureVisible();

        }

        private TreeNode NewTreeNode(ProcessInfo p)
        {
            var treeNode = new TreeNode("[" + p.Id + "] " + p.Name);
            treeNode.Tag = p.Id;
            if (p.Id == CurrentPID)
            {
                treeNode.BackColor = Color.Green;
            }
            return treeNode;
        }

        private TreeNode NewTreeNodeRecursive(ProcessInfo p)
        {
            TreeNode treeNode = NewTreeNode(p);
            if (p.Children != null)
            {
                foreach (ProcessInfo child in p.Children)
                {
                    treeNode.Nodes.Add(NewTreeNodeRecursive(child));
                }
            }
            return treeNode;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            LoadProcess();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void StartAutoRefresh()
        {
            if (timer != null && timer.Enabled)
            {
                return;
            }

            if (timer == null)
            {
                timer = new Timer();
                timer.Tick += Timer_Tick;
                timer.Interval = REFRESH_INTERVAL;
                timer.Start();
            }
            else if (!timer.Enabled)
            {
                timer.Start();
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            LoadProcess();
        }

        private void StopAutoRefresh()
        {
            if (timer != null)
            {
                timer.Stop();
            }
        }

        public void EnableAutoReflush()
        {
            checkBox1.Checked = true;
        }

        public void DisableAutoReflush()
        {
            checkBox1.Checked = false;
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }

        public void EmbedMode()
        {
            splitContainer1.Panel1Collapsed = true;
        }
    }
}
